package com.bibi.zoo.config;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: Wu Shenzhuang
 * @Date: 2019/4/25 14:35
 */
@Configuration
public class RestClientConfig {
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) throws Exception {
        return restTemplateBuilder.requestFactory(() -> {
            CloseableHttpClient httpClient = HttpClientBuilder.create()
                    .setMaxConnTotal(500)
                    .setMaxConnPerRoute(500)
                    .useSystemProperties().build();
            HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
            factory.setConnectionRequestTimeout(3000);
            return factory;
        }).setConnectTimeout(5000).setReadTimeout(5000).build();
    }

}
